window.workspace = function(){
	var response  = <?#$respons?>;
	var sid = {'name':Ext.id(), 'role':Ext.id()};
	var rForm, mtree, store, grid, tlock, win, access, layout;
	function addForm(data){
		if(!win) {
			rForm = new Ext.form.FormPanel({
				frame		: true,
				border		: false,
				width		: 350,
				bodyStyle	: 'padding:8px;',
		        labelWidth	: 80,
		        labelAlign  : 'right',
		    	defaults	: {
		    		xtype	  : 'textfield'
		    	},
		    	items		: [{
		            xtype		: 'hidden',
		            name		: 'id',
		            value		: 0
		    	},{
		            xtype		: 'textfield',
		            fieldLabel	: '角色名',
		       		width		: 180,
		            name		: 'name',
		            regex		: /^[^\s]+$/,
		            regexText	: '角色名不能为空，且不能含有空格',
		            allowBlank	: false
		    	},{
		            xtype		: 'textarea',
		            fieldLabel	: '备注信息',
		            width		: 216,
		            height		: 65,
		            name		: 'remark',
		            allowBlank	: false
		    	}]
			});
			win = new Ext.Window({
				title	: '新增角色信息',
				iconCls	: 'icon-add',	
				width	: 360,
				items	: [rForm],
				closeAction : 'hide',
				bbar 	: {
					xtype		: 'toolbar',
					buttonAlign	: 'right',
					items		: [{
						text	: '保存', 
						iconCls	: 'icon-save',
						url		: '/admin/role/dosave.html',
						handler	: function(){
						if(!rForm.getForm().isValid())
		        			return;
						rForm.getForm().submit({
			        		waitMsg 	: '数据提交处理中...',
							waitTitle 	: '信息提示',
			        		clientValidation: true,
			        		url		: '/admin/role/dosave.html',
				        	success	: function(form, action) {
				        		grid.store.reload();
				        		win.hide();
							},
							failure	: main.submitFailure
				    	});
					}}]
				}
			});
			window.onresize = function () {
				win.center();
			}
		}
		var roleid = Ext.isObject(data)? data.get('id'):0;
		win.setTitle((roleid > 0)? '编辑角色信息':'新增角色信息');
		win.setIconClass((roleid > 0)? 'icon-edit':'icon-add');
		win.show();
		if(roleid < 1) {
			rForm.getForm().reset();	
		} else {
			rForm.getForm().loadRecord(data);
		}
	}
	
	mtree = new Ext.tree.TreePanel({
		width			: 320,
		region			: 'east',
		split       	: true,
		collapseMode	: 'mini',
		bodyBorder  	: false,
		animCollapse	: true,
		useArrows		: true,
		autoScroll		: true,
		animate			: true,
		containerScroll	: true,
		border			: false,
		rootVisible		: false,
		tbar			: [{
			text	: '刷新',
			iconCls	: 'icon-refresh',
			handler : function(){
				mtree.getRootNode().reload();
				mtree.getRootNode().expand(true);
			}
		},'-',{
			text			: '保存',
			iconCls			: 'icon-save',
			url				: '/admin/role/setaccess.html',
			handler: function() {
				var role = Ext.getCmp(sid.role).getValue();
				if(role < 1){
					Ext.Msg.alert('信息提示', '请先点击选中角色，然后选择角色权限保存！');
					return;
				}
				access = mtree.getChecked('id'); 
				access = access.join(',');
		        Ext.Ajax.request({
		        	url 	: '/admin/role/setaccess.html',
		        	params	: {'access':access, 'id':role},
		        	success	: function(resp, req) {
		        		var data = Ext.decode(resp.responseText);
		        		Ext.Msg.alert('信息提示', data.msg);
		        	}
		        });
			}
		},{
			xtype   : 'hidden',
			id		: sid.role,
			value   : 0
		}],
  		listeners 		: {
			'checkchange': function(node, ischecked){
				if(tlock) {
					return;
				}
				tlock = node.id;
				var pnode, isbk=false;
				pnode = node.parentNode;
				while(pnode){
					if(!ischecked && node.leaf) {
						break;
					} 
					if(!ischecked) {
						for(var i = 0; i < pnode.childNodes.length; i++) {
							if(pnode.childNodes[i].attributes.checked) {
								isbk = true;
								break;
							}
						}
						if(isbk) {
							break;
						}
					}
					pnode.getUI().toggleCheck(ischecked);
					pnode.attributes.checked=ischecked;
					pnode = pnode.parentNode;
				};
				node.cascade(function(){
					this.getUI().toggleCheck(ischecked);
					this.attributes.checked=ischecked;
				});
				tlock = false;
			}
		},
		loader 			: {
			dataUrl		: '/admin/menu/dotree.html'
		},
		root			: {
			nodeType	: 'async'
		}
	});
	mtree.getRootNode().expand(true);
	store = new Ext.data.JsonStore({
	    autoDestroy	: true,
	    url			: '/admin/role/dolist.html',
	    remoteSort	: true,
	    sortInfo	: {
	        field	: 'id',
	        direction: 'DESC'
		},
		params		: {
			start	: 0,
			limit	: main.pageSize
		},
		autoLoad		: true,
	    root			: 'dataset',
	 	totalProperty	: 'total',
	    fields			: ['id', 'name', 'remark']
	});
	grid = {
		xtype		: 'grid',
	    store		: store,
		region		: 'center', 
		bodyBorder  : false,
	    columns		: [
			{header: 'ID编号', width:60, sortable:true, dataIndex:'id'},
			{header: '角色名称', width:80, dataIndex:'name'},
			{header: '备注信息', width:160, dataIndex:'remark'}
		],
	    viewConfig	: {
	        forceFit	 : true,
	        scrollOffset : 0
	    },
	    listeners 	:{
	    	'rowdblclick':function(grid, index, e){
		    	var role = grid.store.getAt(index).copy();
		    	addForm(role);
		    },'rowclick':function(grid, index, e){
		    	var role = grid.store.getAt(index);
	    		Ext.getCmp(sid.role).setValue(role.get('id'));
	    		Ext.Ajax.request({
		    		url		: '/admin/role/doaccess.html',
		    		params	: {'id' : role.get('id')},
		    		success: function(resp, req){
		    			var data = Ext.decode(resp.responseText);
		    			if(data.code == 0) {
		    				var root = mtree.getRootNode();
		    					root.cascade(function(){
		    					if(data.access.indexOf(""+this.id) >= 0) {
		    						this.getUI().toggleCheck(true);
					        		this.attributes.checked=true;
		    					} else {
		    						this.getUI().toggleCheck(false);
					        		this.attributes.checked=false;
		    					}
		    				}); 
		    			}
		    		}
	    		});	
	    	},'destroy':function(){
	    		win && win.close();
	    	}
	    },
	    sm			: new Ext.grid.RowSelectionModel({singleSelect:true}),
	    stripeRows	: true,
	    tbar		: [{
				text	: '刷新',
				iconCls	: 'icon-refresh',
				handler : function(){
					store.reload(); 
				}
			},'-',{
				text	: '新增角色',
				iconCls	: 'icon-add',
				url		: '/admin/role/dosave.html',
				handler : function() {
					addForm(0);
				}
			},'-',{
				text	: '删除角色',
				iconCls	: 'icon-delete',
				url		: '/admin/role/doremove.html',
				handler: function() {
					var item = grid.sm.getSelected();
					if(Ext.isEmpty(item)) {
						Ext.Msg.alert('信息提示', '请先点击选中角色，然后再点击这里！');
						return;
					}
					Ext.Msg.confirm('信息提示', '您确认要删除角色？删除将不能恢复。',
					function(btn){
						if(btn != 'yes') {
							return;
						}
				        Ext.Ajax.request({
				        	url 	: '/admin/role/doremove.html',
				        	params	: {'id':item.get('id')},
				        	success	: function(resp, req) {
				        		var data = Ext.decode(resp.responseText);
				        		if(data.code != 0) {
				        			Ext.Msg.alert('信息提示', data.msg); return;
				        		}
				        		store.reload(); 
				        	}
				        });
					});
				}
			},'-','<b>检索资料：</b>','角色名:',{
				xtype       : 'textfield',
				id   		: sid.name,
				width	    : 80
			},{
				text			: '检索',
				iconCls			: 'icon-search',
				handler: function() {
					store.baseParams.name  = Ext.getCmp(sid.name).getValue();
					store.reload(); 
				}
			}],
		bbar		: new Ext.PagingToolbar({
			pageSize	: main.pageSize,
			store		: store,
			displayInfo	: true
		})
	};

	layout = {
		title	: response.title,
		layout	: 'border',
		region	: 'center', 
		items	: [grid, mtree]
	}
	return layout;
}();